package cn.com.lms.business.exam.persistence.repository;

import cn.com.lms.business.exam.persistence.entity.QuestionSubject;


import com.bnzj.core.persistence.repository.BaseRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Repository
public interface QuestionSubjectRepository extends BaseRepository<QuestionSubject,Long> {


    @Transactional
    @Modifying
    @Query(value = "update bnz_exam_question_subject set is_delete =:isDelete WHERE id in (:collection)", nativeQuery = true)
    int updateIsDelete(@Param("isDelete") Boolean isDelete , @Param("collection") Collection<Long> collection) ;


    @Transactional
    @Modifying
    @Query(value = "update bnz_exam_question_subject set question_id =:questionId WHERE id in (:collection)", nativeQuery = true)
    int updateQuestionId(@Param("questionId") Long questionId , @Param("collection") Collection<Long> collection) ;


    @Query(value = "select qs.id subjectId,qs.subject,qs.type,a.answer, parse from  bnz_exam_question_subject qs left join bnz_exam_question_subject_answer a on qs.id=a.subject_id  where qs.is_delete=0 and  qs.question_id  in (:collection)", nativeQuery = true)
    List<Map<String,Object>> selectQuestionSubjectByQuestionIdList(@Param("collection") Collection<Long> collection) ;

    @Query(value = "select qs.id subjectId,qs.subject,qs.type,a.answer, a.parse,(select group_concat(CONCAT(option_name,\" \",content) SEPARATOR '###')   from bnz_exam_question_subject_option so LEFT JOIN bnz_exam_question_subject qs  on so.subject_id=qs.id   where so.subject_id=a.subject_id " +
            "GROUP BY subject_id) options" +
            " from bnz_exam_question_subject qs left join bnz_exam_question_subject_answer a on qs.id=a.subject_id  where qs.is_delete=0 and qs.question_id in (select eq.question_id from bnz_exam_exercise_question eq  where eq.exercise_id= ?1)", nativeQuery = true)
    List<Map<String,Object>> selectQuestionSubjectByExerciseId(Long exerciseId) ;
}
